package bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;

import controller.PersonaleInterface;
import controller.UserMgr;
import dto.ElementoBaseDTO;
import dto.UtenteDTO;

@ManagedBean
@ViewScoped
public class GiftListBean implements Serializable {
	@EJB
	private PersonaleInterface personal;
	@EJB
	private UserMgr userMgr;
	
    private ElementoBaseDTO selectedElemento;
    private ElementoBaseDTO selectedElementoAmico;
	
	public void addGiftList(ElementoBaseDTO selectedElemento){
		UtenteDTO dto = userMgr.getUtenteDTO();
		List<ElementoBaseDTO> giftDTO = dto.getGiftList();
		giftDTO.add(selectedElemento);
		userMgr.update(dto);
        FacesContext facesContext = FacesContext.getCurrentInstance();  
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Elemento inserito nella GiftList:", selectedElemento.getNome()));
	}
	
	public void removeFromGiftList(ElementoBaseDTO selectedElemento){
		UtenteDTO dto = userMgr.getUtenteDTO();
		List<ElementoBaseDTO> giftDTO = dto.getGiftList();
		List<ElementoBaseDTO> newList = new ArrayList<ElementoBaseDTO>();
		for(ElementoBaseDTO ldto : giftDTO){
			if(ldto.getIdElementoBase()!= selectedElemento.getIdElementoBase())
				newList.add(ldto);
		}
		dto.setGiftList(newList);
		userMgr.update(dto);
        FacesContext facesContext = FacesContext.getCurrentInstance();  
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Elemento rimossa dalla GiftList:", selectedElemento.getNome()));
	}
	
	
	
	public void removeFromGiftList(){
		UtenteDTO dto = userMgr.getUtenteDTO();
		List<ElementoBaseDTO> giftDTO = dto.getGiftList();
		List<ElementoBaseDTO> newList = new ArrayList<ElementoBaseDTO>();
		for(ElementoBaseDTO ldto : giftDTO){
			if(ldto.getIdElementoBase()!= selectedElemento.getIdElementoBase())
				newList.add(ldto);
		}
		dto.setGiftList(newList);
		userMgr.update(dto);
        FacesContext facesContext = FacesContext.getCurrentInstance();  
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Elemento rimossa dalla GiftList:", selectedElemento.getNome()));
	}
	
	public void removeFromGiftListAmico(String email){
		UtenteDTO dto = userMgr.getUtenteEmail(email);
		if(dto!=null){
		List<ElementoBaseDTO> giftDTO = dto.getGiftList();
		List<ElementoBaseDTO> newList = new ArrayList<ElementoBaseDTO>();
		for(ElementoBaseDTO ldto : giftDTO){
			if(ldto.getIdElementoBase()!= selectedElemento.getIdElementoBase())
				newList.add(ldto);
		}
		dto.setGiftList(newList);
		userMgr.update(dto);
		}
	}

	public ElementoBaseDTO getSelectedElemento() {
		return selectedElemento;
	}

	public void setSelectedElemento(ElementoBaseDTO selectedElemento) {
		this.selectedElemento = selectedElemento;
	}

	public ElementoBaseDTO getSelectedElementoAmico() {
		return selectedElementoAmico;
	}

	public void setSelectedElementoAmico(ElementoBaseDTO selectedElementoAmico) {
		System.out.println("Sto settando");
		if(selectedElementoAmico!=null)	System.out.println(selectedElementoAmico.getNome());
		else System.out.println("Null");
		this.selectedElementoAmico = selectedElementoAmico;
	}
	
	
	

}
